package com.acegear.horizon.controllers;

import com.acegear.horizon.controllers.vo.ResultVO;
import com.acegear.horizon.domain.repository.CommentRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangsike on 2017/3/1.
 */
@RequestMapping("/comments")
@RestController
public class CommentController {
    @Autowired
    private CommentRepository commentRepository;

    @RequestMapping(value = "/{commentId}/report", method = RequestMethod.POST)
    @Transactional
    public ResultVO reportComment(
            @PathVariable Long commentId,
            @RequestHeader(value = "X-Consumer-Username") Long userId) {
        return commentRepository.findCommentId(commentId)
                .map(comment -> comment.report(userId))
                .map(ResultVO::new)
                .orElse(ResultVO.COMMENT_NOT_FOUND);
    }
}
